package com.android.utils;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.NetworkInfo.DetailedState;
import android.net.Uri;
import android.telephony.TelephonyManager;

public class PhoneUtils
{
	public static enum ConnectionType
	{
		WI_FI,
		MOBILE_DATA,
		NO_CONNECTION;
	}
	
	public static void makeCall(Activity activity, String number) throws ActivityNotFoundException
	{
		Intent callIntent = new Intent(Intent.ACTION_CALL);
		callIntent.setData(Uri.parse("tel:" + number));
		activity.startActivity(callIntent);
	}

	public static void sendEmail(Activity activity, String email, String subject, String body) throws ActivityNotFoundException
	{
		Intent emailIntent = new Intent(Intent.ACTION_SEND);
		emailIntent.setType("text/plain");
		emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[] { email });
		emailIntent.putExtra(Intent.EXTRA_TEXT, body);
		emailIntent.putExtra(Intent.EXTRA_SUBJECT, subject);
		activity.startActivity(Intent.createChooser(emailIntent, "Email"));
	}
	
	public static void sendEmail(Activity activity, String email) throws ActivityNotFoundException
	{
		Intent emailIntent = new Intent(Intent.ACTION_SEND);
		emailIntent.setType("text/plain");
		emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[] { email });
		activity.startActivity(Intent.createChooser(emailIntent, "Email"));
	}
	
	public static String getImei(Context context)
	{
		TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
		return telephonyManager.getDeviceId();
	}
	
	public static ConnectionType connectionType(Context context)
	{
		try
		{
			ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
			
			NetworkInfo wifi = connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
			
			NetworkInfo mobile = connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
			
			if (wifi.isAvailable() && wifi.getDetailedState() == DetailedState.CONNECTED)
			{
				return ConnectionType.WI_FI;
			}
			else if (mobile.isAvailable() && mobile.getDetailedState() == DetailedState.CONNECTED)
			{
				return ConnectionType.MOBILE_DATA;
			}
			else
			{
				return ConnectionType.NO_CONNECTION;
			}
		}
		catch (Exception e)
		{
			return ConnectionType.NO_CONNECTION;
		}
	}
	
	public static boolean isOnline(Context context)
	{
		ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo netInfo = cm.getActiveNetworkInfo();
		if (netInfo != null && netInfo.isConnectedOrConnecting())
		{
			return true;
		}
		return false;
	}
	
	public static String getAppVersion(Context context)
	{
		try
		{
			return context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionName;
		}
		catch (Exception e)
		{
			return "1.0";
		}
	}
	
}
